<?php
namespace app\home\controller;
use \think\Db;


class Article extends \think\Controller
{
    public function index(){
        //新闻资讯类别
        $category = Db('baode_article_category')->where('title','新闻资讯')->find();
        $categoryArr = Db('baode_article_category')->where('pid',intval($category['id']))->select();
        input('category_id') and $category_id = input('category_id') or $category_id = $categoryArr[0]['id'];
        $where = 'is_show = 1 and category_id = '.intval($category_id);
        $category_id and $data = Db('baode_article')->where($where)->order('is_top asc ,id desc')->paginate(10);
        $aboutArr = $data->items();
        $pages = $data->render();
        return $this->allFetch(get_defined_vars());
    }
    
    public function info(){
        //新闻资讯类别
        $category = Db('baode_article_category')->where('title','新闻资讯')->find();
        $categoryArr = Db('baode_article_category')->where('pid',intval($category['id']))->select();
        input('category_id') and $category_id = input('category_id') or $category_id = $categoryArr[0]['id'];
        input('id') and $id = input('id');
        $where = 'is_show = 1 and category_id = '.intval($category_id).' and id = '.intval($id);
        $articleInfo = Db('baode_article')->where($where)->find();
        
        //点击量
        $views = $articleInfo['views'] + 1;
        Db('baode_article')->where('id', $articleInfo['id'])->update(['views' => $views]);
        
        //上一篇
        $where1 = 'category_id = '.intval($category_id).' and id < '.$articleInfo['id'];
        $sInfo = Db('baode_article')->where($where1)->order('id desc')->find();

        //下一篇
        $where2 = 'category_id = '.intval($category_id).' and id > '.$articleInfo['id'];
        $xInfo = Db('baode_article')->where($where2)->order('id asc')->find();

        
        
        return $this->allFetch(get_defined_vars());
    }
    
}
